<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          @search="handleSearch"
        />

        <el-button type="primary" @click="handleSearch">查询</el-button>

        <el-button
          type="primary"
          v-if="uidPermissions.includes('village:userpoint:export')"
          @click="handleExport"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="subDistrict"
          label="街道"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="communityName"
          label="社区"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="villageName"
          label="小区"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="buildingNo"
          label="楼号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="unitNo"
          label="单元号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="houseNumber"
          label="门牌号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="inPoint"
          label="累积增加的积分"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="outPoint"
          label="累积扣减的积分"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="currentPoint"
          label="当前积分"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="memo"
          label="积分说明"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="uidPermissions.includes('village:userpoint:update')"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form ref="form" :model="form" label-width="auto" :rules="rules">
        <el-form-item label="积分修改类型" prop="type">
          <!-- <el-input v-model="form.name"></el-input> -->
          <el-radio v-model="form.type" label="0">增加</el-radio>
          <el-radio v-model="form.type" label="1">减少</el-radio>
        </el-form-item>
        <el-form-item label="积分" prop="point">
          <el-input v-model="form.point"></el-input>
        </el-form-item>
        <el-form-item label="修改原因" prop="reason">
          <!-- <el-input v-model="form.point"></el-input> -->
          <el-select v-model="form.reason" placeholder="请选择" clearable>
            <el-option
              v-for="item in form.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import NProgress from "nprogress";
import { mapGetters } from "vuex";
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
import util from "utils/util";
export default {
  name: "userintegrationdata",
  data() {
    return {
      uidPermissions: [],
      rules: {
        type: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        point: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        reason: [{ required: true, message: "必填不能为空", trigger: "blur" }],
      },
      query: {
        pageIndex: 1,
        pageSize: 20,
        streetId: "",
        communityId: "",
        villageId: "",
        buildingNum: "",
        unitNum: "",
        houseNum: "",
      },
      streetList: [],
      communityList: [],
      villageList: [],
      tableData: [],

      pageTotal: 0,
      editVisible: false,
      form: {
        id: "",
        type: "0",
        point: "",
        reason: "",
        options: [
          {
            value: "投放获取积分",
            label: "投放获取积分",
          },
        ],
      },
    };
  },
  created() {
    this.getData();
    this.uidPermissions = this.permissions;
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "form.type"(newType, oldType) {
      console.log(newType, oldType);
      if (newType == "0") {
        this.form.reason = "";
        this.form.options = [
          {
            value: "投放获取积分",
            label: "投放获取积分",
          },
        ];
      } else if (newType == "1") {
        this.form.reason = "";
        this.form.options = [
          {
            value: "0",
            label: "提现",
          },
          {
            value: "1",
            label: "购物",
          },
          {
            value: "2",
            label: "分类错误",
          },
          {
            value: "3",
            label: "恶意投放",
          },
          {
            value: "4",
            label: "其他",
          },
        ];
      }
    },
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/user-integration-data") {
        this.uidPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      this.uidPermissions = newVal;
    },
  },
  methods: {
    ...commonItem1,
    // 编辑
    handleEdit(index, row) {
      this.editVisible = true;
      this.form.id = row.id;
    },
    cancelEdit() {
      this.editVisible = false;
      this.form = {
        id: "",
        type: "0",
        point: "",
        reason: "",
        options: [
          {
            value: "投放获取积分",
            label: "投放获取积分",
          },
        ],
      };
    },
    saveEdit() {
      var bool = util.isEmpty({
        type: this.form.type,
        point: this.form.point,
        reason: this.form.reason,
      });
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      if (!/^[1-9]\d*$/.test(this.form.point)) {
        return this.$message.warning("积分必须为正整数");
      }
      this.$axios
        .put("/village/userpoint", {
          id: this.form.id,
          type: this.form.type,
          point: this.form.point,
          reason: this.form.reason,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("修改成功");
          this.editVisible = false;
          this.form = {
            id: "",
            type: "0",
            point: "",
            reason: "",
            options: [
              {
                value: "投放获取积分",
                label: "投放获取积分",
              },
            ],
          };
        })
        .catch((err) => {});
    },
    // 获取数据
    getData() {
      this.$axios
        .get("/village/userpoint/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            villageId: this.query.villageId,
            buildingNo: this.query.buildingNum,
            unitNo: this.query.unitNum,
            houseNumber: this.query.houseNum,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          res.data.list.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          this.tableData = res.data.list;
          this.pageTotal = res.data.total || 0;
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    handleExport() {
      NProgress.start();
      this.$axios
        .get("/village/userpoint/export", {
          params: {
            villageId: this.query.villageId,
            buildingNo: this.query.buildingNum,
            unitNo: this.query.unitNum,
            houseNumber: this.query.houseNum,
          },
          responseType: "blob",
        })
        .then((res) => {
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.error({
              message: "导出失败",
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch(() => {});
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: auto;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
